<template>
  <d2-container>
    <div class="flex-1">
      <div class="ml-2">
        <el-button type="warning" icon="el-icon-plus" size="small" @click="toAddCoupon">添加优惠券</el-button>
      </div>
      <div class="flex-1 mt-2">
        <el-table :data="couponList" stripe size="medium">
          <el-table-column type="expand" width="20">
            <template slot-scope="props">
              <div class="flex-1 mx-3">
                <el-form label-position="left" inline class="demo-table-expand">
                  <el-form-item label="优惠券名称">
                    <span>{{ props.row.title }}</span>
                  </el-form-item>
                  <el-form-item label="开始时间">
                    <span class="text-danger">{{ props.row.start_datetime }}</span>
                  </el-form-item>
                  <el-form-item label="过期时间">
                    <span class="text-danger">{{ props.row.end_datetime }}</span>
                  </el-form-item>
                  <el-form-item label="添加时间">
                    <span>{{ props.row.create_time }}</span>
                  </el-form-item>
                </el-form>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="序号" type="index" width="25" />
          <el-table-column align="center" label="优惠券名称" prop="title" width="80" />
          <el-table-column align="center" label="发放数量" prop="coupon_count" width="50" />
          <el-table-column align="center" label="剩余数量" prop="coupon_residue" width="50" />
          <el-table-column align="center" label="优惠金额" prop="discount_amount" width="50" />
          <el-table-column align="center" label="使用金额" prop="minimum_order_amount" width="50" />
          <el-table-column align="center" label="状态" prop="coupon_examine" width="60">
            <template slot-scope="scope">
              <el-tag size="small" v-if="scope.row.coupon_status == 0" type="success" effect="dark">正常 </el-tag>
              <el-tag size="small" v-if="scope.row.coupon_status == 1" type="danger" effect="dark">下架</el-tag>
              <el-tag size="small" v-if="scope.row.coupon_status == 2" type="danger" effect="dark">领完</el-tag>
              <el-tag size="small" v-if="scope.row.coupon_status == 3" type="danger" effect="dark">过期</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="100">
            <template slot-scope="scope">
              <el-button size="mini">使用详情</el-button>
              <el-button size="mini" @click="onClick(scope.row.id)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex-1 ml-2 mt-2">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page.sync="queryInfo.page" layout="total, prev, pager, next" :total="couponTotal">
        </el-pagination>
      </div>
    </div>
  </d2-container>
</template>

<script>
  import {
    couponListUrl
  } from '@/utils/promotion'
  export default {
    data() {
      return {
        queryInfo: {
          page: 1,
          page_size: 10
        },
        couponList: [],
        couponTotal: 0
      }
    },
    created() {
      this.init()
    },
    methods: {
      init() {
        this.getCouponList()
      },
      getCouponList() {
        this.$get(couponListUrl, this.queryInfo).then(res => {
          this.couponList = res.data.data.list
        })
      },
      toAddCoupon() {
        this.$router.push({
          path: '/coupon/addCoupon',
        })
      },
      onClick(couponId) {
        this.$router.push({
          path: '/coupon/addCoupon',
          query: {
            couponId: couponId // 添加第二个参数
          }
        })
      },
      // 分页页码显示条数
      handleSizeChange(newSize) {
        // 把每页显示多少数据重新赋值
        this.queryInfo.page_size = newSize
        this.getCouponList()
      },
      // 翻页事件
      handleCurrentChange(newPage) {
        this.queryInfo.page = newPage
        this.getCouponList()
      },
    }
  }
</script>

<style>
</style>